package com.dhcc.bpm.modules.content.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dhcc.bpm.common.annotation.Log;
import com.dhcc.bpm.common.enums.BusinessType;
import com.dhcc.bpm.common.utils.SafeHtmlUtils;
import com.dhcc.bpm.modules.content.entity.Marketing;
import com.dhcc.bpm.modules.content.service.IMarketingService;
import com.dhcc.bpm.modules.content.vo.MarketingVO;
import com.dhcc.bpm.modules.system.service.ISerialNumberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.log.annotation.ApiLog;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @ClassName MarketingController
 * @Description  营销信息管理 控制器
 * @Author lijiacheng
 * @Date 2020/2/21 14:16
 * @Version V1.0
 * @Copyright: 2020 东华软件 . All rights reserved.
 */

@RestController
@AllArgsConstructor
@RequestMapping("bpm-content/marketing")
@Api(value = "营销信息表", tags = "营销信息表接口")
public class MarketingController extends BladeController {

	private IMarketingService marketingService;

	private ISerialNumberService serialNumberService;

	/**
	 * 详情
	 */
	@ApiLog("营销信息模块表详情")
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入marketing")
	public R<Marketing> detail(Marketing marketing) {
		Marketing detail = marketingService.getOne(Condition.getQueryWrapper(marketing));
		return R.data(detail);
	}

	/**
	 * 分页 营销信息表
	 */
	@ApiLog("营销信息模块表列表")
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入marketing")
	public R<IPage<MarketingVO>> list(MarketingVO marketing, Query query) {
		IPage<MarketingVO> pages = marketingService.selectMarketingPage(Condition.getPage(query), marketing);
		return R.data(pages);
	}

	/**
	 * 自定义分页 营销信息表
	 */
	@ApiLog("营销信息模块表分页")
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入marketing")
	public R<IPage<MarketingVO>> page(MarketingVO marketing, Query query) {
		IPage<MarketingVO> pages = marketingService.selectMarketingPage(Condition.getPage(query), marketing);
		return R.data(pages);
	}

	/**
	 * 新增 营销信息表
	 */
	@ApiLog("营销信息模块表新增")
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入marketing")
	@Log(title = "营销信息新增", businessType = BusinessType.INSERT)
	public R save(@Valid @RequestBody Marketing marketing) {
		String safeHtml = SafeHtmlUtils.getSafeHtml(marketing.getMarketingContent());
		marketing.setMarketingContent(safeHtml);
		return R.status(marketingService.save(marketing));
	}

	/**
	 * 修改 营销信息表
	 */
	@ApiLog("营销信息模块表更新")
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入marketing")
	@Log(title = "营销信息更新", businessType = BusinessType.UPDATE)
	public R update(@Valid @RequestBody Marketing marketing) {
		String safeHtml = SafeHtmlUtils.getSafeHtml(marketing.getMarketingContent());
		marketing.setMarketingContent(safeHtml);
		return R.status(marketingService.updateById(marketing));
	}

	/**
	 * 新增或修改 营销信息表
	 */
	@ApiLog("营销信息模块表提交")
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入marketing")
	@Log(title = "营销信息", businessType = BusinessType.INSERT)
	public R submit(@Valid @RequestBody Marketing marketing) {
		if(marketing.getId() == null){
			// 设置信息编号
			String code = serialNumberService.generateSerialNumberByModelCode("opc_marketing");
			marketing.setMarketingCode(code);
		}
		//空指针处理 修改时间 20200914 17:02
		if(marketing.getMarketingContent() != null){
			String safeHtml = SafeHtmlUtils.getSafeHtml(marketing.getMarketingContent());
			marketing.setMarketingContent(safeHtml);
		}
		return R.status(marketingService.saveOrUpdate(marketing));
	}

	
	/**
	 * 删除 营销信息表
	 */
	@ApiLog("营销信息模块表删除")
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	@Log(title = "营销信息删除", businessType = BusinessType.DELETE)
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(marketingService.deleteLogic(Func.toLongList(ids)));
	}

	
}
